﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Rapor
{
    public partial class xrIrsaliyeOrtakDetail : DevExpress.XtraReports.UI.XtraReport
    {
        public xrIrsaliyeOrtakDetail()
        {
            InitializeComponent();
        }
        public void fillTables(string rolls)
        {
            myData11.Merge(Session.DataManager.ExecuteDataSet("GetIrsaliyeFromRollId", rolls));
            // myData1.Irsaliye.Merge(myData1.Tables["Table"]);
            myData11.IrsaliyeLines.Merge(myData11.Tables["Table1"]);
            myData11.IrsaliyeLinesDetail.Merge(myData11.Tables["Table2"]);
        }
        int KesilecekTop;
        public void fillTables(DataTable dt, int _kesilecekTop)
        {
            //myData11.Merge(Session.DataManager.ExecuteDs("GetCikisEmri", cikisEmriId));
            //myData11.Irsaliye.Merge(myData11.Tables["Table"]);
            //myData11.IrsaliyeLines.Merge(myData11.Tables["Table1"]);
            myData11.IrsaliyeLinesDetail.Merge(dt);
            KesilecekTop = _kesilecekTop;
        }

        private void xrSubDetail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (KesilecekTop == 0)
            {
                subKesilecek.Dispose();
                subRaf.Dispose();
            }
        }

        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 9.75f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.Width / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        private void xrTableCell8_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }


        private void xrTableCell6_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrTableCell6.ForeColor = Color.Black;
            DataRow dr = myData11.IrsaliyeLinesDetail.Select("RollId=" + GetCurrentColumnValue("RollId").ToString())[0];
            if (dr["ProductId"] != dr["AlternatifId"] && dr["AlternatifId"] != DBNull.Value && KesilecekTop != 0)
            {
                //myData11.IrsaliyeLinesDetail.Select("RollId=" + GetCurrentColumnValue("RollId").ToString())[0]["RollId"] = 0;//(int)myData11.IrsaliyeLinesDetail.Select("RollId=" + GetCurrentColumnValue("RollId").ToString())[0]["RollId"] * -1;
                xrTableCell6.ForeColor = Color.Red;
                xrTableCell6.Text = "*" + xrTableCell6.Text;
            }
        }

        private void xrTableCell2_BeforePrint_1(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcMarka_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }
    }
}
